package server

import (
	"errors"
	"fmt"
	"github.com/google/uuid"
	"gorm.io/gorm"
	"onerk4/yk/common/dao"
	"onerk4/yk/common/model"
	"onerk4/yk/common/pkg"
	"onerk4/yk/common/proto/user"
)

func UserLogin(req *user.UserLoginRequest) (*user.UserLoginResponse, error) {
	userInfo, err := dao.QueryUserByUsername(req.Username)
	if err != nil {
		return nil, err
	}

	if userInfo.Password != req.Password {
		return nil, errors.New("密码错误")
	}

	claims := pkg.CustomClaims{ID: userInfo.ID}
	token, err := pkg.NewJWT("2209a").CreateToken(claims)
	if err != nil {
		return nil, errors.New("token生成失败")
	}

	return &user.UserLoginResponse{Success: token}, nil
}

func AddCourse(req *user.AddCourseRequest) (*user.AddCourseResponse, error) {
	c := model.Course{
		Model:         gorm.Model{},
		CourseName:    req.CourseName,
		CourseInfo:    req.CourseInfo,
		CourseContent: req.CourseContent,
		CoursePrice:   req.CoursePrice,
		Status:        int(req.Status),
	}
	err := dao.AddCourse(&c)
	if err != nil {
		return nil, errors.New("发布课程失败")
	}

	return &user.AddCourseResponse{Success: "发布成功"}, nil
}

func CourseList(req *user.CourseListRequest) (*user.CourseListResponse, error) {
	list, err := dao.QueryCourseList()
	if err != nil {
		return nil, errors.New("列表展示失败")
	}

	var item []*user.CourseItem

	for _, course := range list {
		item = append(item, &user.CourseItem{
			Id:            int64(course.ID),
			CourseName:    course.CourseName,
			CourseInfo:    course.CourseInfo,
			CourseContent: course.CourseContent,
			CoursePrice:   course.CoursePrice,
			Status:        int64(course.Status),
		})
	}

	return &user.CourseListResponse{List: item}, nil
}

func CourseInfo(req *user.CourseInfoRequest) (*user.CourseInfoResponse, error) {
	courseInfo, err := dao.QueryCourseInfoById(req.Id)
	if err != nil {
		return nil, errors.New("查询详情失败")
	}

	return &user.CourseInfoResponse{
		Id:            int64(courseInfo.ID),
		CourseName:    courseInfo.CourseName,
		CourseInfo:    courseInfo.CourseInfo,
		CourseContent: courseInfo.CourseContent,
		CoursePrice:   courseInfo.CoursePrice,
		Status:        int64(courseInfo.Status),
	}, nil
}

func UpdateCourse(req *user.UpdateCourseRequest) (*user.UpdateCourseResponse, error) {
	c := model.Course{
		CourseName:    req.CourseName,
		CourseInfo:    req.CourseInfo,
		CourseContent: req.CourseContent,
		CoursePrice:   req.CoursePrice,
		Status:        int(req.Status),
	}
	err := dao.UpdateCourse(req.Id, c)
	if err != nil {
		return nil, errors.New("修改失败")
	}
	return &user.UpdateCourseResponse{Success: "修改成功"}, nil
}

func AddCoursePanic(req *user.AddCoursePanicRequest) (*user.AddCoursePanicResponse, error) {
	courseInfo, err := dao.QueryCourseInfoById(req.CourseId)
	if err != nil {
		return nil, errors.New("查询详情失败")
	}

	c := model.CoursePanic{
		Model:       gorm.Model{},
		CourseId:    int(req.CourseId),
		CourseName:  courseInfo.CourseName,
		CourseInfo:  courseInfo.CourseInfo,
		CoursePrice: courseInfo.CoursePrice,
		Num:         int(req.Num),
		StartTime:   req.StartTime,
		EndTime:     req.EndTime,
		Status:      int(req.Status),
	}

	err = dao.AddCoursePanic(&c)
	if err != nil {
		return nil, errors.New("发布抢购课程失败")
	}

	return &user.AddCoursePanicResponse{Success: "发布抢购课程成功"}, nil
}

func AddOrder(req *user.AddOrderRequest) (*user.AddOrderResponse, error) {
	courseInfo, err := dao.QueryCourseInfo(req.CourseId)
	if err != nil {
		return nil, errors.New("查询详情失败")
	}

	if courseInfo.Num <= 0 {
		return nil, errors.New("课程已被抢空")
	}

	if req.Num > 1 {
		return nil, errors.New("只能购买一个")
	}

	orderSn := uuid.New().String()
	o := model.Order{
		Model:       gorm.Model{},
		OrderSn:     orderSn,
		CourseId:    int(req.CourseId),
		CourseName:  courseInfo.CourseName,
		CourseInfo:  courseInfo.CourseInfo,
		CoursePrice: courseInfo.CoursePrice,
		Num:         int(req.Num),
		Status:      1,
	}
	err = dao.AddOrder(&o)
	if err != nil {
		return nil, errors.New("抢购失败")
	}

	sprintf := fmt.Sprintf("%.2f", courseInfo.CoursePrice)
	pay := pkg.AliPay(courseInfo.CourseName, orderSn, sprintf)

	return &user.AddOrderResponse{Success: pay}, nil
}
